package com.quanyan.factory;

import static org.jboss.netty.channel.Channels.pipeline;

import com.quanyan.util.Configure;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;

import com.quanyan.handler.CommandDecoder;
import com.quanyan.handler.CommandEncoder;
import com.quanyan.handler.ServerHandler;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.HashedWheelTimer;

/**
 * 
 * @ClassName: ServerPipelineFactory
 * @Description: 工厂类
 * @author chuntu tuchun168@163.com
 * @date 2016年5月3日 上午11:18:42
 *
 */
public class ServerPipelineFactory implements ChannelPipelineFactory {
	private final HashedWheelTimer hashedWheelTimer = new HashedWheelTimer();
	//读操作空闲
	private final static int readerIdleTimeSeconds = Integer.parseInt(Configure.getPropertyBykey("readerIdle_timeSeconds"));
	//写操作空闲
	private final static int writerIdleTimeSeconds = Integer.parseInt(Configure.getPropertyBykey("writerIdle_timeSeconds"));
	//读写全部空闲
	private final static int allIdleTimeSeconds = Integer.parseInt(Configure.getPropertyBykey("allIdle_timeSeconds"));
	public ChannelPipeline getPipeline() throws Exception {

		ChannelPipeline pipeline = pipeline();

		pipeline.addLast("idle", new IdleStateHandler(hashedWheelTimer, readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds));
		// 解码
		pipeline.addLast("decoder", new CommandDecoder());

		// 业务
		pipeline.addLast("handler", new ServerHandler());
		//编码
		pipeline.addLast("encoder", new CommandEncoder());

		return pipeline;
	}
}
